package main

import (
	"fmt"
	"log"

	"zhihu-api/config"
	"zhihu-api/models"
	"zhihu-api/routes"

	"github.com/gin-gonic/gin"
)

func main() {
	// 加载配置
	config.Init()

	// 设置数据库连接
	models.SetupDB()

	// 创建Gin引擎
	r := gin.Default()

	// 设置路由
	routes.Setup(r)

	// 启动服务
	port := config.AppConfig.Server.Port
	if port == 0 {
		port = 8080
	}

	serverAddr := fmt.Sprintf(":%d", port)
	log.Printf("Server starting on %s", serverAddr)
	if err := r.Run(serverAddr); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
}
